module.exports = app => {
	const express = require('express');
	const router = express.Router();
  	const jwt = require('jsonwebtoken');
  	
	// 获取资源列表
	router.get('/', async (req, res) => {
		const data=await req.Service._select(req.query);
		res.json(data);
	})
	// 获取资源详情 (数量)
	.get('/:name', async (req, res) => {
		const data=await req.Service[req.params.name]();
		res.json(data);
	})
	// 创建资源
	.post('/', async (req, res) => {
		const result = await req.Service._create(req.body);
		res.json(result);
	})
	// 更新资源
	.put('/', async (req, res) => {
		const result = await req.Service._update(req.body);
		if(result.affectedRows>0) res.json({success:true});  
		else res.json({success:false});  
	})
	// 删除资源
	.delete('/', async (req, res) => {
	 	const result = await req.Service._delete(req.body.id);
		if(result.affectedRows>0) res.json({success:true});  
		else res.json({success:false});  
	})
	const authMiddleware = require('../../middleware/auth')
	const resourceMiddleware = require('../../middleware/resource');
	app.use('/api/admin/:resource',authMiddleware(),resourceMiddleware(), router);
}